package com.website.company.controller.api;

import com.website.company.entity.CompanyDevelopment;
import com.website.company.entity.CompanyInfo;
import com.website.company.entity.Navigation;
import com.website.company.entity.Person;
import com.website.company.mapper.CompanyDevelopmentMapper;
import com.website.company.mapper.PersonMapper;
import com.website.company.service.CompanyInfoService;
import com.website.company.service.NavigationService;
import com.website.company.service.PersonService;
import com.website.company.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@Controller
public class SubInfoController {

    @Autowired
    private CompanyDevelopmentMapper mapper;
    @Autowired
    NavigationService navigationService;
    @Autowired
    CompanyInfoService companyInfoService;
    @Autowired
    UserService userService;
    @Resource
    private PersonMapper personMapper;
    //新闻详情页
    @GetMapping("/subDetail")
    public String subDetail(String subId, String key, Model model) throws IOException, InterruptedException {
        setLeftMode(model);
        model.addAttribute("sub", mapper.getById(Integer.parseInt(subId)));
        List<Person> list = personMapper.getProductList();
        model.addAttribute("persons",list);
        return "showPage/message/sub_detail";
    }

    /**
     *  获取左侧导航需要的数据
     * @param model 视图
     */
    private void setLeftMode(Model model) {
        long start = System.currentTimeMillis();
        Thread t1 = new Thread(() -> {
            List<Navigation> navVOS = navigationService.showAllNav();
            model.addAttribute("navList", navVOS);
        });
        Thread t2 = new Thread(() -> {
            //联系方式
            CompanyInfo companyDetail = companyInfoService.getCompanyDetail();
            model.addAttribute("linkUs", userService.show(companyDetail.getLinkManId()));
        });
        Thread t8 = new Thread(()-> {
            List<CompanyDevelopment> list = mapper.getList();
            model.addAttribute("subList", list);
        });
        t1.start();
        t2.start();
        t8.start();
        try {
            t1.join();
            t2.join();
            t8.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("消耗时间----->：" + (end - start));
    }
}
